package com.cz.video.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cz.video.common.constant.UserConstants;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.website.domain.WebsitePlayLogs;
import com.cz.video.website.domain.WebsitePlayLogs;
import com.cz.video.website.domain.WebsitePlayLogs;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.website.domain.bo.WebsitePlayLogsBo;
import com.cz.video.website.domain.bo.WebsitePlayLogsBo;
import com.cz.video.website.domain.vo.WebsitePlayLogsVo;
import com.cz.video.website.mapper.WebsitePlayLogsMapper;
import com.cz.video.website.service.IWebsitePlayLogsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 * 播放记录表 服务实现类
 * </p>
 *
 * @author LLF
 * @since 2023-10-16
 */
@Service
@RequiredArgsConstructor
public class WebsitePlayLogsServiceImpl extends ServiceImpl<com.cz.video.website.mapper.WebsitePlayLogsMapper, WebsitePlayLogs> implements IWebsitePlayLogsService {

    private final WebsitePlayLogsMapper websitePlayLogsMapper;

    /**
     * 新增保存播放记录表信息
     *
     * @param websitePlayLogs 用户信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPlayLogs(WebsitePlayLogs websitePlayLogs) {
        int rows = websitePlayLogsMapper.insert(websitePlayLogs);
        return rows;
    }

    /**
     * 修改播放记录表信息
     * @param websitePlayLogs
     * @return
     */
    @Override
    public int updatePlayLogs(WebsitePlayLogsBo websitePlayLogs) {
        return websitePlayLogsMapper.updateById(websitePlayLogs);
    }

    /**
     * 分页查找播放记录表信息
     *
     * @param websitePlayLogs
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<WebsitePlayLogsVo> selectPagePlayLogsList(WebsitePlayLogsBo websitePlayLogs, PageQuery pageQuery) {
        Page<WebsitePlayLogsVo> page=websitePlayLogsMapper.selectPagePlayLogsList(pageQuery.build(),this.buildQueryWrapper(websitePlayLogs));
        return TableDataInfo.build(page);
    }

    @Override
    public int deleteById(int id) {
        return websitePlayLogsMapper.deleteById(id);
    }

    /**
     * 根据查询条件构建query wrapper
     *
     * @param websitePlayLogs 查询条件
     *
     * @return query wrapper
     */
    private Wrapper<WebsitePlayLogs> buildQueryWrapper(WebsitePlayLogsBo websitePlayLogs) {
        Map<String, Object> params = websitePlayLogs.getParams();
        QueryWrapper<WebsitePlayLogs> wrapper = Wrappers.query();

        wrapper.eq("pl.del_flag", UserConstants.USER_NORMAL)
            .eq(ObjectUtil.isNotNull(websitePlayLogs.getPlayLogId()), "pl.PlayLogs_id", websitePlayLogs.getPlayLogId())
            .eq(ObjectUtil.isNotNull(websitePlayLogs.getCustomId()), "pl.Custom_id", websitePlayLogs.getCustomId())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "pl.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }
}
